import requests
import json


def get_data(link_arg=None):
    """
    数据请求，返回热榜数据
    :param link_arg:
    :return:
    """
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    # 发送请求
    res = requests.get(link_arg, headers=headers)
    # 获取响应数据
    json_data = list()
    if res and res.status_code == 200:
        json_data = res.json()["data"]
        if len(json_data) > 0:
            # 迭代响应数据，拆分其内容
            for d in json_data:
                item = dict()
                item["articleDetailUrl"] = d["articleDetailUrl"]  # 文章地址
                item["articleTitle"] = d["articleTitle"]  # 文章标题
                item["avatarUrl"] = d["avatarUrl"]  # 头像地址
                item["commentCount"] = d["commentCount"]  # 评论数量
                item["favorCount"] = d["favorCount"]  # 收藏数量
                item["hotRankScore"] = d["hotRankScore"]  # 热度完整数字
                item["nickName"] = d["nickName"]  # 昵称
                item["pcHotRankScore"] = d["pcHotRankScore"]  # 热度简写
                item["userName"] = d["userName"]  # 用户名
                item["viewCount"] = d["viewCount"]  # 浏览量
                yield item


if __name__ == '__main__':
    base_url = 'https://blog.csdn.net/phoenix/web/blog/hot-rank?page={}&pageSize=25'
    hot_rank_links = [base_url.format(page) for page in range(50)]
    rank_data = list()
    for link in hot_rank_links:
        rank_data.extend(list(get_data(link)))

    filename = 'c_rank.json'          #use the file extension .json
    with open(filename, 'w') as file_object:  #open the file in write mode
        json.dump(rank_data, file_object)   # json.dump() function to stores the set of numbers in numbers.json file
